package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rrd.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.oneconnect.OneConnectRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Date;

/**
 *壹账通-好信欺诈资讯2.0
 *
 * @author renzhanzheng
 */

public class OneConnectFraudnewsParse extends  ParseBase {

    private Text val = new Text();
    private Text key = new Text();

    /**
     *壹账通-好信欺诈资讯2.0
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        OneConnectRecord obj = (OneConnectRecord) model;
        if (!("." + obj.getServiceName()).equals(getSubType())) {
            return false;
        }
        Date date = new Date(obj.getTimestamp());
        if (date.before(DateUtil.formateToDate("2020-07-22 19:00:00"))) {
            return false;
        }

        JSONObject info = obj.getData() == null ? new JSONObject() : JSON.parseObject(obj.getData());

        StringBuilder sb = getBaseInfo(obj, successful);

        //错误代码
        sb.append(info.getString("erCode")).append(CRT001);
        //错误信息
        sb.append(info.getString("erMsg")).append(CRT001);
        //手机号码
        sb.append(info.getString("mobileNo")).append(CRT001);
        //IP地址
        sb.append(info.getString("ip")).append(CRT001);
        //序列号
        sb.append(info.getString("seqNo")).append(CRT001);
        //命中暴力破解
        sb.append(info.getString("isMachdForce")).append(CRT001);
        //命中DNS服务器
        sb.append(info.getString("isMachdDNS")).append(CRT001);
        //命中邮件服务器
        sb.append(info.getString("isMachdMailServ")).append(CRT001);
        //命中seo
        sb.append(info.getString("isMachdSEO")).append(CRT001);
        //命中组织出口
        sb.append(info.getString("isMachdOrg")).append(CRT001);
        //命中爬虫
        sb.append(info.getString("isMachdCrawler")).append(CRT001);
        //命中代理服务器
        sb.append(info.getString("isMachdProxy")).append(CRT001);
        //命中第三方标注黑名单
        sb.append(info.getString("isMachdBlacklist")).append(CRT001);
        //命中web服务器
        sb.append(info.getString("isMachdWebServ")).append(CRT001);
        //命中vpn服务器
        sb.append(info.getString("isMachdVPN")).append(CRT001);
        //风险评分
        sb.append(info.getString("rskScore")).append(CRT001);
        //ip风险时间
        sb.append(info.getString("iUpdateDate")).append(CRT001);
        //省份
        sb.append(info.getString("province")).append(CRT001);
        //城市
        sb.append(info.getString("city")).append(CRT001);
        //运营商
        sb.append(info.getString("isp")).append(CRT001);
        //命中虚假手机号
        sb.append(info.getString("isMachRskNo")).append(CRT001);
        //虚假手机号更新时间
        sb.append(info.getString("rskUpdateTime")).append(CRT001);
        //命中通信小号
        sb.append(info.getString("isMachSmallNo")).append(CRT001);
        //通信小号最近检测到时间
        sb.append(info.getString("smallNoUpdateTime")).append(CRT001);
        //命中标记号码
        sb.append(info.getString("isMachTagNo")).append(CRT001);
        //标记号码类型
        sb.append(info.getString("tagNoType")).append(CRT001);
        //号码标记时间
        sb.append(info.getString("tagNoUpdateTime")).append(CRT001);
        //命中网贷平台
        sb.append(info.getString("isMachNetLoanNo")).append(CRT001);
        //网贷平台名称
        sb.append(info.getString("netLoanName")).append(CRT001);
        //网贷平台更新时间
        sb.append(info.getString("netLoanUpdateTime")).append(CRT001);
        //是否虚拟运营商手机号
        sb.append(info.getString("isMachVirtualNo")).append(CRT001);
        //虚拟号类型
        sb.append(info.getString("virtualNoType")).append(CRT001);
        //虚拟运营商
        sb.append(info.getString("virtualIsp")).append(CRT001);
        //预留字段1
        sb.append(info.getString("extFiled1")).append(CRT001);
        //预留字段2
        sb.append(info.getString("extFiled2")).append(CRT001);
        //预留字段3
        sb.append(info.getString("extFiled3")).append(CRT001);

        key.set(getKey());
        val.set(sb.toString().replace("\n", "").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getSubType() {
        return ".msc8624";
    }

    @Override
    public String getTableName() {
        return "kafka_babel_oneconnect_fraudnews_inc";
    }

    @Override
    public String getType() {
        return RecordType.ONE_CONNECT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "壹账通-好信欺诈资讯2.0";
    }
}
